<?php
namespace app\index\controller;

use app\model\Members;
use MoCommon\Business\BizCodes;
use MoCommon\DataState\TradeLogPayIssueStatus;
use MoCommon\DataState\TradeLogPayType;
use MoCommon\DataState\TradeLogStatus;
use MoCommon\DataState\TradeLogType;
use MoCommon\Support\Codes;
use MoCommon\Support\Helper;
use app\index\Thirdparty\WxpayCoreTrait;

class Trade extends Apibase
{
    private static $memberModel;
    public function __construct()
    {
        self::$memberModel=new Members();
    }

    use WxpayCoreTrait;

    /**
     * 一对一付费下单
     */
    public function onetoone()
    {
        //验签
        parent::sign_validate();
        $json = '';
        $role_id = parent::getUid();
        $question_id = input('question_id');
        $price = input('price');
        $to_uid = input('to_uid');
        $id = input('trade_id');
        if (($question_id && $price && $to_uid) || $id) {
            if (isset($to_uid) && ($to_uid == $role_id)) {
                $status = BizCodes::QUESTION_CAN_ASK_SELF;
                $msg = BizCodes::get($status);
                return parent::respond([], $status, $msg);
            }

            if (isset($id)) {
                $trade_res = self::$memberModel->findTrade(['id' => $id]);

                if ($trade_res) {
                    $param = $trade_res;
                    if (($param['status'] == TradeLogStatus::HAS_PAID)
                        || ($param['uid'] != $role_id)
                        || ($param['pay_issue_status'] != TradeLogPayIssueStatus::ASKING)
                    ) {
                        $status = Codes::ACTION_ILL;
                        $msg = Codes::get($status);
                        return parent::respond([], $status, $msg);
                    }
                    unset($param['status']);
                } else {
                    $status = Codes::ACTION_ILL;
                    $msg = Codes::get($status);
                    return parent::respond([], $status, $msg);
                }

            } else {
                $param = [
                    'question_id' => $question_id,
                    'price' => $price,
                    'openid' => parent::userInfo()['openid'],
                    'to_uid' => $to_uid,
                    'to_openid' => self::$memberModel->findMembers(["id = '{$to_uid}'"]),
                    'type' => TradeLogType::ONE_TO_ONE,
                    'pay_type' => TradeLogPayType::WXPAY,
                    'out_trade_no' => Helper::orderid($role_id),
                    'body' => TradeLogType::get(TradeLogType::ONE_TO_ONE),
                    'uid' => $role_id,
                    'addtime' => $_SERVER['REQUEST_TIME'],
                    'pay_issue_status' => TradeLogPayIssueStatus::ASKING,
                ];

                $id = self::$memberModel->insertTrade($param);
            }

            if ($id) {
                $param['notify_url'] = "http://onlineapi.meditool.cn/api/onetoonenotify";

                $json = $this->prepay($param);

                if ($json) {
                    $status = Codes::ACTION_SUC;
                    $msg = Codes::get($status);
                } else {
                    $status = BizCodes::TRADE_SENDPAY_FAIL;
                    $msg = BizCodes::get($status);
                }
            } else {
                $status = BizCodes::TRADE_GEN_FAIL;
                $msg = BizCodes::get($status);
            }

        } else {
            $status = Codes::PARAM_ERR;
            $msg = Codes::get($status);
        }


        return parent::respond([
            'bridge_json_string' => $json,
        ], $status, $msg);
    }


}
